package com.gdit.zax.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.dao.DynamicDao;
import com.gdit.zax.entitys.Comment;
import com.gdit.zax.entitys.CrouseTree;
import com.gdit.zax.entitys.Dynamic;
import com.gdit.zax.entitys.MyStudentDym;
import com.gdit.zax.entitys.Record;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.LogUtil;
import com.gdit.zax.utils.SqlUtil;

public class DynamicDaoImpl implements DynamicDao {
	private PreparedStatement ps;
	private ResultSet rs;
	
	private Connection conn;
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	
	public DynamicDaoImpl(Connection conn,HttpServletRequest request, HttpServletResponse response) {
		this.conn = conn;
		this.request = request;
		this.response = response;
	}

	public boolean addSystemDynamic(int type, int userid, int videoid,String content, String pic) {
		try {
			String sql = "insert t_dynamic set DynType=?,UserId=?,VideoId=?,Pic=?,Content=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, type);
			ps.setInt(2, userid);
			ps.setInt(3, videoid);
			ps.setString(4, pic);
			ps.setString(5,content);
			
			int result = ps.executeUpdate();
			if (result!=0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public ArrayList<Dynamic> getMySystemDynamic(int uid, int page,int onepagecount) {
		String sql = SqlUtil.getSql("getSystemDymicall_My.sql");
		
		ArrayList<Dynamic> list = new ArrayList<Dynamic>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, (page-1)*onepagecount);
			ps.setInt(3, onepagecount);
			
			rs = ps.executeQuery();
			
			while(rs.next()){
				Dynamic dyn = DBUtil.getobject(rs, Dynamic.class);
				list.add(dyn);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	

	public ArrayList<Dynamic> getSystemDynamic(int page, int onepagecount) {
		ArrayList<Dynamic> list = new ArrayList<Dynamic>();
		try {
			String sql = "select * from t_dynamic limit ?,?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (page-1)*onepagecount);
			ps.setInt(2, onepagecount);
			
			rs = ps.executeQuery();
			
			while(rs.next()){
				Dynamic dyn = DBUtil.getobject(rs, Dynamic.class);
				list.add(dyn);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public boolean addCommDynamic(int userid, Comment comm) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean addDownDynamic(int userid, Record rec) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean addLearnDynamic(int userid, Record rec) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean addSeeDynamic(int userid, Record rec) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean addVideoDynamic(int userid, Dynamic dym) {
		// TODO Auto-generated method stub
		return false;
	}

	public ArrayList<Dynamic> getAllDynamic(int page, int onepagecount) {
		return null;
	}

	public ArrayList<Comment> getCommDynamic(int page, int onepagecount) {
		// TODO Auto-generated method stub
		return null;
	}

	public ArrayList<Dynamic> getDownDynamic(int page, int onepagecount) {
		// TODO Auto-generated method stub
		return null;
	}

	public ArrayList<Dynamic> getLearnDynamic(int page, int onepagecount) {
		return null;
	}

	public ArrayList<Dynamic> getMyAllDynamic(int uid, int page,int onepagecount) {
		String sql = SqlUtil.getSql("getalldymicall.sql");
		
		ArrayList<Dynamic> list = new ArrayList<Dynamic>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, uid);
			ps.setInt(3, uid);
			ps.setInt(4, uid);
			ps.setInt(5,(page-1)*onepagecount);
			ps.setInt(6, onepagecount);
			
			rs = ps.executeQuery();
			
			while(rs.next()){
				Dynamic dyn = DBUtil.getobject(rs, Dynamic.class);
				list.add(dyn);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public int getMyAllDynamicsum(int uid) {
		
		return 0;
	}

	public ArrayList<Comment> getMyCommDynamic(int uid, int page,int onepagecount) {
		return null;
	}

	public int getMyCommDynamicsum(int uid) {
		// TODO Auto-generated method stub
		return 0;
	}

	public ArrayList<Dynamic> getMyDownDynamic(int uid, int page,
			int onepagecount) {
		return null;
	}

	public int getMyDownDynamicsum(int uid) {
		// TODO Auto-generated method stub
		return 0;
	}

	public ArrayList<Dynamic> getMyLearnDynamic(int uid, int page,
			int onepagecount) {
		// TODO Auto-generated method stub
		return null;
	}

	public int getMyLearnDynamicsum(int uid) {
		// TODO Auto-generated method stub
		return 0;
	}

	public ArrayList<Dynamic> getMySeeDynamic(int uid, int page,int onepagecount) {
		String sql = SqlUtil.getSql("getSeeRecord_My.sql");
		
		ArrayList<Dynamic> list = new ArrayList<Dynamic>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			
			rs = ps.executeQuery();
			
			while(rs.next()){
				Dynamic dyn = DBUtil.getobject(rs, Dynamic.class);
				list.add(dyn);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public int getMySeeDynamicsum(int uid) {
		// TODO Auto-generated method stub
		return 0;
	}

	public int getMySystemDynamicsum(int uid) {
		// TODO Auto-generated method stub
		return 0;
	}

	public ArrayList<Dynamic> getMyVideoDynamic(int uid, int page,
			int onepagecount) {
		// TODO Auto-generated method stub
		return null;
	}

	public int getMyVideoDynamicsum(int uid) {
		// TODO Auto-generated method stub
		return 0;
	}

	public ArrayList<Dynamic> getSeeDynamic(int page, int onepagecount) {
		// TODO Auto-generated method stub
		return null;
	}

	public ArrayList<Dynamic> getVideoDynamic(int page, int onepagecount) {
		// TODO Auto-generated method stub
		return null;
	}

	public ArrayList<CrouseTree> getUserLearnTree(int uid, int page,int onepagecount) {
		String sql = SqlUtil.getSql("getUserTree.sql");
		ArrayList<CrouseTree> list = new ArrayList<CrouseTree>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, uid);
			ps.setInt(3,(page-1)*onepagecount);
			ps.setInt(4, onepagecount);
			
			rs = ps.executeQuery();
			
			while(rs.next()){
				CrouseTree dyn = DBUtil.getJoinObject(rs, CrouseTree.class);
				list.add(dyn);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public ArrayList<Dynamic> getProcToday(int uid, int page, int onepagecount) {
		String sql = SqlUtil.getSql("getproctoday.sql");
		ArrayList<Dynamic> list =new ArrayList<Dynamic>();
			
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, uid);
			ps.setInt(3,(page-1)*onepagecount);
			ps.setInt(4, onepagecount);
			
			rs = ps.executeQuery();
			
			while(rs.next()){
				Dynamic dyn = DBUtil.getobject(rs, Dynamic.class);
				list.add(dyn);
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public ArrayList<MyStudentDym> getMyStuDynamic(int uid, int page,
			int onepagecount) {
		String sql = SqlUtil.getSql("getMyStudentDym.sql");
		ArrayList<MyStudentDym> list =new ArrayList<MyStudentDym>();
			
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2,(page-1)*onepagecount);
			ps.setInt(3, onepagecount);
			
			rs = ps.executeQuery();
			
			while(rs.next()){
				MyStudentDym dyn = DBUtil.getJoinObject(rs, MyStudentDym.class);
				list.add(dyn);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public int getMyStuDynamicsum(int uid) {
		// TODO Auto-generated method stub
		return 0;
	}

	


}
